/*

Copyright (C) 2010 Hans-Werner Hilse <hilse@web.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

*/

package de.hilses.droidreader;

import android.graphics.RectF;

/**
 * references a page within a PdfDocument
 */
public class PdfPage {
	/**
	 * the rotation that the page wants to apply
	 */
	public int rotate;
	/**
	 * the number of this page
	 */
	public int no;
	/**
	 * the MediaBox for the page, still as float[4]
	 */
	protected float[] mMediabox = {0, 0, 0, 0};

	/**
	 * the boundaries of the content for the page, still as float[4]
	 */
	protected float[] mContentbox = {0, 0, 0, 0};

	/**
	 * this will be used to store a C Pointer (ick!) to the
	 * structure holding our references in the native code
	 */
	protected long mHandle = 0;

	/**
	 * calls the native code to open a page
	 * @param dochandle the Handle (C pointer) for the document
	 * @param mediabox this will be set to the mediabox of the page
	 * @param no page number to open
	 * @return handle for the opened page
	 */
	private native long nativeOpenPage(long dochandle, float[] mediabox, float[] contentbox, int no, int flags)
		throws PageLoadException;

	/**
	 * constructs a new PdfPage object for a given page in a given document
	 * @param doc the PdfDocument
	 * @param no the number of the page (starting at 1) to open
     * @param flags 1 if the document is a memory hog
	 */
	public void open(PdfDocument doc, int no, int flags)
			throws PageLoadException
	{
		if(mHandle != 0)
			this.close();
		this.no = no;
		mHandle = this.nativeOpenPage(doc.mHandle, mMediabox, mContentbox, no, flags);
	}

	/**
	 * cleans up the memory we claimed in native code
	 * @param pagehandle the handle we got upon opening the page
	 */
	private native void nativeClosePage(long pagehandle);

	/**
	 * clean up memory. Note that the object must not be used afterwards!
	 */
	public void close() {
		if(mHandle != 0) {
			this.nativeClosePage(mHandle);
			mHandle = 0;
			no = 0;
		}
	}

	/**
	 * destructor caring for cleaning up memory
	 */
	public void finalize() {
		this.close();
	}

	/**
	 * getter for the MediaBox of the opened page
	 * @return RectF holding the MediaBox
	 */
	public RectF getMediaBox() {
		return new RectF(mMediabox[0], mMediabox[1], mMediabox[2], mMediabox[3]);
	}

	/**
	 * getter for the content boundaries of the opened page
	 * @return RectF holding the boundaries
	 */
	public RectF getContentBox() {
		return new RectF(mContentbox[0], mContentbox[1], mContentbox[2], mContentbox[3]);
	}
}
